$(function () {
  var layer = layui.layer;
  var form = layui.form;
  var laypage = layui.laypage;

  //----------------------------------------------------------------
  // 页面查询参数
  var query = {
    pagenum: 1, //页码值
    pagesize: 5, //每页显示几条数据
    username: "", //姓名
    sno: "", //学号
  };
  //----------------------------------------------------------------
  //渲染分页功能
  function renderPage(total) {
    // console.log(total);
    laypage.render({
      elem: "pageBox", //分页容器的id
      count: total, //数据总数，从服务端得到
      limit: query.pagesize, //每页显示几条数据
      curr: query.pagenum,
      layout: ["count", "limit", "prev", "page", "next", "skip"],
      limits: [5, 10, 15, 20],
      jump: function (obj, first) {
        // console.log(first);
        query.pagenum = obj.curr;
        query.pagesize = obj.limit;
        // 首行不执行
        if (!first) {
          initUsersList();
        }
      },
    });
  }
  //----------------------------------------------------------------
  initUsersList();

  //----------------------------------------------------------------
  //表单验证
  form.verify({
    pwd: [/^[\S]{6,12}$/, "密码必须6到12位，且不能出现空格"],
    repwd: function (value) {
      var pwd = $("#form_addUser [name=password]").val();
      // console.log(pwd);
      if (pwd != value) {
        return "两次密码不一致！";
      }
    },
    edit_repwd: function (value) {
      var pwd = $("#form_editUser [name=password]").val();
      // console.log(pwd);
      if (pwd != value) {
        return "两次密码不一致！";
      }
    },
    role: [/S/, "请选择角色"],
    sno: [/^[\S]{9}$/, "学号或工号必须9位，且不能出现空格"],
  });
  //----------------------------------------------------------------
  //获取所有用户信息列表
  function initUsersList() {
    var username = $("#form_search [name = username]").val();
    var sno = $("#form_search [name = sno]").val();
    query.username = username;
    query.sno = sno;
    $.ajax({
      method: "POST",
      url: "/my/userinfo/usersOB/",
      data: query,
      success: function (res) {
        var htmStr = template("tpl-table", res);
        $("tbody").html(htmStr);
        renderPage(res.total);
      },
    });
  }
  //----------------------------------------------------------------
  //所有用户信息模糊搜索需要的信息
  $("#form_search").on("submit", function (e) {
    e.preventDefault();
    initUsersList();
  });
});
